package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.service.MarketCategoryService;
import com.cskaoyan.market.util.Constant;
import com.cskaoyan.market.util.MybatisUtil;
import com.cskaoyan.market.vo.CategoryLevelVo;
import com.cskaoyan.market.vo.MarketCategoryVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author CMQ
 * @since 2023/07/24 19:32
 */

public class MarketCategoryServiceImpl implements MarketCategoryService {

    /**
     * 参考今天上午老师讲的goods的insert方法
     * @return
     */
    @Override
    public List<MarketCategoryVo> list() {
        SqlSession session = MybatisUtil.getSession();
        MarketCategoryMapper categoryMapper = session.getMapper(MarketCategoryMapper.class);
        //获取所有类目的信息
        List<MarketCategory> categoryList = categoryMapper.selectByExample(new MarketCategoryExample());
        session.commit();
        session.close();
        //根据pid分组，查询目录的所有层级
        Map<Integer, List<MarketCategory>> categoryGroupByPidMap = groupByPid(categoryList);
        // 一级目录
        List<MarketCategory> l1CategoryList = categoryGroupByPidMap.get(Constant.FIRST_TYPE);
        List<MarketCategoryVo> l1CategoryVoList = new ArrayList<>();
        for (MarketCategory l1Category : l1CategoryList) {
            MarketCategoryVo l1CategoryVO = new MarketCategoryVo(l1Category.getId(), l1Category.getName(), l1Category.getKeywords(), l1Category.getDesc(), l1Category.getPid(), l1Category.getIconUrl(), l1Category.getPicUrl(), l1Category.getLevel(), l1Category.getSortOrder(), l1Category.getAddTime(), l1Category.getUpdateTime(), l1Category.getDeleted(), null);
            //二级目录
            List<MarketCategoryVo> l2CategoryVoList = new ArrayList<>();
            List<MarketCategory> secondCategoryList = categoryGroupByPidMap.get(l1Category.getId());
            // 第一次没有数据，为空
            if (secondCategoryList != null) {
                for (MarketCategory secondCategory : secondCategoryList) {
                    MarketCategoryVo l2CategoryVO = new MarketCategoryVo(secondCategory.getId(), secondCategory.getName(), secondCategory.getKeywords(), secondCategory.getDesc(), secondCategory.getPid(), secondCategory.getIconUrl(), secondCategory.getPicUrl(), secondCategory.getLevel(), secondCategory.getSortOrder(), secondCategory.getAddTime(), secondCategory.getUpdateTime(), secondCategory.getDeleted(), null);
                    l2CategoryVoList.add(l2CategoryVO);
                }
            }
            l1CategoryVO.setChildren(l2CategoryVoList);
            l1CategoryVoList.add(l1CategoryVO);
        }
        return l1CategoryVoList;
    }

    private Map<Integer, List<MarketCategory>> groupByPid(List<MarketCategory> categoryList) {
        Map<Integer, List<MarketCategory>> categoryGroupByPidMap = new HashMap<>();
        for (MarketCategory marketCategory : categoryList) {
            Integer pid = marketCategory.getPid();
            List<MarketCategory> categoryListOfPid = categoryGroupByPidMap.get(pid);
            if (categoryListOfPid == null) {
                categoryListOfPid = new ArrayList<>();
                categoryGroupByPidMap.put(pid, categoryListOfPid);
            }
            categoryListOfPid.add(marketCategory);
        }
        return categoryGroupByPidMap;
    }

    @Override
    public boolean delete(Integer id) {
        SqlSession session = MybatisUtil.getSession();
        MarketCategoryMapper categoryMapper = session.getMapper(MarketCategoryMapper.class);
        //根据id删除数据
        categoryMapper.deleteByPrimaryKey(id);
        session.commit();
        session.close();
        return true;
    }

    @Override
    public MarketCategory create(MarketCategory category) {
        SqlSession session = MybatisUtil.getSession();
        MarketCategoryMapper marketCategoryMapper = session.getMapper(MarketCategoryMapper.class);
        category.setAddTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        marketCategoryMapper.insert(category);
        session.commit();
        session.close();
        return category;
    }

    @Override
    public MarketCategory update(Integer id, String name, String desc, String level, String picUrl, String iconUrl, String keywords, String addTime, String updateTime) {
        SqlSession session = MybatisUtil.getSession();
        MarketCategoryMapper categoryMapper = session.getMapper(MarketCategoryMapper.class);

        // 根据id查询到需要更新的商品类目
        MarketCategory marketCategory = categoryMapper.selectByPrimaryKey(id);

        // 判空
        if (StringUtils.isEmpty(picUrl)) {
            marketCategory.setPicUrl(null);
        }
        if (StringUtils.isEmpty(picUrl)) {
            marketCategory.setIconUrl(null);
        }
        marketCategory.setName(name);
        marketCategory.setDesc(desc);
        marketCategory.setLevel(level);
        marketCategory.setKeywords(keywords);
        marketCategory.setUpdateTime(LocalDateTime.now());
        marketCategory.setAddTime(LocalDateTime.now());

        categoryMapper.updateByPrimaryKey(marketCategory);
        session.commit();
        session.close();

        return marketCategory;
    }

    @Override
    public List<CategoryLevelVo> l1() {
        SqlSession session = MybatisUtil.getSession();
        MarketCategoryMapper mapper = session.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andLevelEqualTo("L1");
        criteria.andDeletedEqualTo(false);
        marketCategoryExample.setOrderByClause("add_time" + " " + "desc");
        List<MarketCategory> marketCategories = mapper.selectByExample(marketCategoryExample);

        // 封装
        List<CategoryLevelVo> categoryLevelVos = new ArrayList<>();

        for (MarketCategory marketCategory : marketCategories) {
            CategoryLevelVo categoryLevelVo = new CategoryLevelVo();
            categoryLevelVo.setLabel(marketCategory.getLevel());
            categoryLevelVo.setValue(marketCategory.getId());
            categoryLevelVos.add(categoryLevelVo);
        }

        session.commit();
        session.close();

        return categoryLevelVos;

    }


}
